//Copyright (c) Microsoft Corporation. All rights reserved.
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using NUnit.Framework;

namespace Microsoft.Office.OneNote.UnitTests
{
	/// <summary>
	/// Tests the NotebookRoot class.
	/// </summary>
	[TestFixture]
	public class NotebookRootTest
	{
		[Test]
		public void Construction()
		{
			Assert.IsTrue(OneNote.Application.NotebookRoot != null);
		}

		[Test]
		public void CanContain()
		{
			NotebookRoot root = OneNote.Application.NotebookRoot;
			for (int iChild = 0; iChild < root.GetChildCount(); iChild++)
			{
				Assert.IsTrue(root.GetChild(iChild) is Notebook || root.GetChild(iChild) is UnfiledNotes);
			}
		}

		[Test]
		public void OpenNotebook()
		{
			NotebookRoot root = OneNote.Application.NotebookRoot;

			// Let's create a new notebook:
			Notebook testNotebook = TestUtils.CreateEmptyNotebook();

			// We should handle duplicates:
			using (HierarchyTransaction addDuplicateNotebookTransaction = new HierarchyTransaction(root))
			{
				root.AppendChild(new Notebook(testNotebook.Path));
				addDuplicateNotebookTransaction.Commit();

				// Confirm that only one notebook exists in our object model:
				int countOfNotebooks = 0;
				for (int iChild = 0; iChild < root.GetChildCount(); iChild++)
				{
					ANode child = root.GetChild(iChild);
					if ((child is Notebook) && ((Notebook) child).Path == testNotebook.Path)
						countOfNotebooks++;
				}

				Assert.IsTrue(countOfNotebooks == 1);
			}

			TestUtils.CloseAndDeleteNotebook(testNotebook);
		}

		[Test]
		public void CloseNotebook()
		{
			NotebookRoot root = OneNote.Application.NotebookRoot;

			// Let's create a new notebook:
			Notebook testNotebook = TestUtils.CreateEmptyNotebook();

			int notebookCount = root.GetChildCount();

			// Close the notebook:
			TestUtils.CloseAndDeleteNotebook(testNotebook);

			// Is it really closed?
			using (HierarchySynchronizationLocker synchronizationLock = new HierarchySynchronizationLocker(root))
			{
				Assert.IsTrue(root.GetChildCount() == notebookCount - 1);
				for (int iNotebook = 0; iNotebook < root.GetChildCount(); iNotebook++)
				{
					ANode child = root.GetChild(iNotebook);
					Assert.IsTrue(!(child is Notebook) || ((Notebook) child).Path != testNotebook.Path);
				}
			}
		}

		[Test]
		public void ReorderNotebook()
		{
			NotebookRoot root = OneNote.Application.NotebookRoot;

			// Let's create a new notebook:
			Notebook testNotebook = TestUtils.CreateEmptyNotebook();

			int notebookPosition = -1;
			using (HierarchySynchronizationLocker synchronizationLock = new HierarchySynchronizationLocker(root))
			{
				for (int iNotebook = 0; iNotebook < root.GetChildCount(); iNotebook++)
				{
					if (root.GetChild(iNotebook) == testNotebook)
					{
						notebookPosition = iNotebook;
						break;
					}
				}
			}

			// Great!  Now let's reorder it:
			if (notebookPosition > 0)
			{
				using (HierarchyTransaction reorderNotebookTransaction = new HierarchyTransaction(root))
				{
					root.InsertChild(testNotebook, 0);
					reorderNotebookTransaction.Commit();

					notebookPosition = -1;
					for (int iNotebook = 0; iNotebook < root.GetChildCount(); iNotebook++)
					{
						if (root.GetChild(iNotebook) == testNotebook)
						{
							notebookPosition = iNotebook;
							break;
						}
					}

					Assert.IsTrue(notebookPosition == 0);
				}
			}

			TestUtils.CloseAndDeleteNotebook(testNotebook);
		}
	}
}
